package com.nut.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nut.pojo.Brand;
import com.nut.pojo.BrandExample;
import com.nut.pojo.Color;
import com.nut.pojo.ColorExample;
import com.nut.pojo.ProductQuery;
import com.nut.pojo.ProductWithBLOBs;
import com.nut.pojo.Sku;
import com.nut.pojo.SkuExample;
import com.nut.pojo.Storage;
import com.nut.pojo.StorageExample;
import com.nut.service.brand.BrandServiceI;
import com.nut.service.color.ColorServiceI;
import com.nut.service.product.ProductServiceI;
import com.nut.service.sku.SkuServiceI;
import com.nut.service.storage.StorageServiceI;

/**
 * @author Nut
 * @version 2018年8月15日 下午4:37:16
 * @TODO
 *
 */
//@RestController
@Controller
@RequestMapping("/product")
public class ProductController {

	@Resource
	BrandServiceI brandService;
	@Resource
	ColorServiceI colorService;
	@Resource
	StorageServiceI storageService;
	@Resource
	ProductServiceI productService;
	@Resource
	SkuServiceI skuService;

	
	@ResponseBody
	@RequestMapping("/getData.do")
	public Map<String, List<?>> getData() {
		Map<String, List<?>> map = new HashMap<>();
		/* 查询所有的品牌对象 */
		Brand brand = new Brand();
		brand.setWithPage(0);
		BrandExample brandExample = new BrandExample();
		brandExample.createCriteria().andIsDelEqualTo(1);
		map.put("brands", brandService.query(brand, brandExample));
		/* 查询所有的颜色 */
		ColorExample colorExample = new ColorExample();
		colorExample.createCriteria().andIsDelEqualTo(1);
		map.put("colors", colorService.getColors(colorExample));
		/* 查询所有的内存 */
		StorageExample storageExample = new StorageExample();
		storageExample.createCriteria().andIsDelEqualTo(1);
		map.put("storages", storageService.getStorages(storageExample));
		return map;
	}
	
	/*
	 * 前端多条件查询
	 * 如果无条件时，则显示所有商品
	 */
	@ResponseBody
	@RequestMapping("/getProduct.do")
	public List<ProductWithBLOBs> getProduct(ProductQuery productQuery,ModelMap map){
		/*//设置分页开启
		productQuery.setWithPage(1);
		//分页需要的条件
		StringBuilder params = new StringBuilder();
		if(productQuery.getBrandId() !=0){
			params.append("&brandId"+productQuery.getBrandId());
		}
		if(productQuery.getColorId() != 0){
			params.append("&colorId"+productQuery.getColorId());
		}
		if(productQuery.getStorageId() !=0){
			params.append("&storageId"+productQuery.getStorageId());
		}
		
		Page<ProductWithBLOBs> pages = (Page<ProductWithBLOBs>) productService.search(productQuery);
		
		
		productQuery.setUrl("index.do");
		productQuery.setPages(pages.getPages());
		productQuery.setParams(params.toString());
		
		
		map.addAttribute("productQuery", productQuery);*/
		productQuery.setWithPage(0);
		return productService.search(productQuery);
	}
	
	//进入商品详情
	@RequestMapping("/goProduct.do")
	public String goProduct(String id,ModelMap map){
		map.addAttribute("id", id);
		System.out.println("######### "+id);
		return "product";
	}
	
	//返回商品的详细信息
	@RequestMapping("/getProductDetails.do")
	@ResponseBody
	public ProductWithBLOBs getProduct(int id) {
		ProductWithBLOBs product = productService.queryOne(id);
		
		ColorExample colorExample = new ColorExample();
		StorageExample storageExample = new StorageExample();
		List<Integer> colors = new ArrayList<>();
		List<Integer> storages = new ArrayList<>();
		String[] colorarr = product.getColorList().split(",");
		for (String string : colorarr) {
			colors.add(Integer.parseInt(string));
		}
		colorExample.createCriteria().andIdIn(colors);
		List<Color> colorlist = colorService.getColors(colorExample);
		
		String[] storagearr = product.getStorageList().split(",");
		for (String string : storagearr) {
			storages.add(Integer.parseInt(string));
		}
		storageExample.createCriteria().andIdIn(storages);
		List<Storage> storagelist = storageService.getStorages(storageExample);
		
		product.setColors(colorlist);
		product.setStorages(storagelist);
		SkuExample skuExample = new SkuExample();
		skuExample.createCriteria().andProductIdEqualTo(id);
		List<Sku> skus = skuService.query(skuExample);
		product.setSkus(skus);
		return product;
	}
}
